package com.smbms.controller;

import com.mysql.cj.util.StringUtils;
import com.smbms.pojo.Provider;
import com.smbms.pojo.User;
import com.smbms.service.BillService;
import com.smbms.service.ProviderService;
import com.smbms.tools.Constants;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/provider")
public class ProviderController {
    @Resource
    private ProviderService providerService;

    @Resource
    private BillService billService;

    @GetMapping(value = "/list.html")
    public String providerList(@RequestParam(required = false) String queryProCode,
                               @RequestParam(required = false) String queryProName,
                               HttpServletRequest request) {

        List<Provider> pList = providerService.getProviderList(queryProName, queryProCode);

        request.setAttribute("providerList", pList);
        return "providerlist";
    }

    @GetMapping("/add.html")
    public String toAdd() {
        return "provideradd";
    }

    @PostMapping(value = "/addsave.html")
    public String proSave(@Valid Provider provider, BindingResult bindingResult, HttpServletRequest request) {
        if (bindingResult.hasErrors()) {
            return "provideradd";
        }
        User logUser = (User) request.getSession().getAttribute(Constants.USER_SESSION);

        provider.setCreatedBy(logUser.getId());
        provider.setCreationDate(new Date());
        System.out.println(provider.getProName());
        boolean result = providerService.add(provider);
        if (result) {
            return "redirect:/provider/list.html";
        }
        return "provideradd";
    }

    @RequestMapping(value = "/modify", method = RequestMethod.GET)
    public String modify(@RequestParam String proid, Model model) {
        Provider provider = providerService.getProviderById(proid);
        model.addAttribute(provider);
        return "providermodify";
    }

    @RequestMapping(value = "/modifysave.html", method = RequestMethod.POST)
    public String prosave(Provider provider, HttpServletRequest request) {
        User loginUser = (User) request.getSession().getAttribute(Constants.USER_SESSION);
        provider.setModifyBy(loginUser.getId());
        provider.setModifyDate(new Date());
        boolean result = providerService.modify(provider);
        System.out.println(result);
        if (result) {
            return "redirect:/provider/list.html";
        }
        return "providermodify";
    }

    @RequestMapping(value = "/delPro", method = RequestMethod.GET)
    @ResponseBody
    public Object delPro(@RequestParam String delId) {
        HashMap<String, Object> resultMap = new HashMap<>();
        if (StringUtils.isNullOrEmpty(delId)) {
            resultMap.put("delResult", "notexist");
        } else {
            int sum = billService.countByProId(delId);
            if (sum == 0) {
              int result =providerService.deleteProviderById(delId);
              if (result>0){
                  resultMap.put("delResult", "true");
              }else {
                  resultMap.put("delResult", "false");
              }
            } else {
                resultMap.put("delResult", sum);
            }
        }
        return resultMap;
    }

    @RequestMapping(value = "/proview", method = RequestMethod.GET)
    public String proView(@RequestParam String proid, Model model) {
        Provider provider = providerService.getProviderById(proid);
        model.addAttribute(provider);
        return "providerview";
    }
}
